<template>
  <Form ref="fm" :validation-schema="rules" v-slot="{ errors }" class="xtx-form"  autocomplete="off">
    <!-- 测试 -->
    <!-- <p>{{ errors }}</p> -->
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field v-model="formData.account" name="account" class="input" :class="{error:errors.account }" type="text" placeholder="请输入用户名" />
      </div>
      <!-- 表单验证错误信息提示 -->
        <div class="error" v-show="errors.account">
          <i class="iconfont icon-warning" />{{ errors.account }}
        </div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field v-model="formData.mobile" name="mobile" class="input"  :class="{error:errors.mobile }" type="text" placeholder="请输入手机号" />
      </div>
      <!-- 表单验证错误信息提示 -->
        <div class="error" v-show="errors.mobile">
          <i class="iconfont icon-warning" />{{ errors.mobile }}
        </div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field v-model="formData.code" name="code" class="input"  :class="{error:errors.code }" type="text" placeholder="请输入验证码" />
        <span class="code" @click="handlerCode">{{ count === 0 ? '发送验证码' : `${count}秒后重发` }}</span>
      </div>
      <!-- 表单验证错误信息提示 -->
        <div class="error" v-show="errors.code">
          <i class="iconfont icon-warning" />{{ errors.code }}
        </div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field v-model="formData.password" name="password" class="input"  :class="{error:errors.password }" type="password" placeholder="请输入密码" />
      </div>
      <!-- 表单验证错误信息提示 -->
        <div class="error" v-show="errors.password">
          <i class="iconfont icon-warning" />{{ errors.password }}
        </div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field v-model="formData.rePassword" name="rePassword"  class="input"  :class="{error:errors.rePassword }" type="password" placeholder="请确认密码" />
      </div>
      <!-- 表单验证错误信息提示 -->
        <div class="error" v-show="errors.rePassword">
          <i class="iconfont icon-warning" />{{ errors.rePassword }}
        </div>
    </div>
    <a href="javascript:;" class="submit" @click="submit" >立即提交</a>
  </Form>
</template>

<script>
import { Form, Field } from 'vee-validate'
import { reactive } from 'vue'
import myRules from '@/utils/vee-validate-schema'

import { userQQPatchCode, userQQPatchLogin } from '@/api/user'
import { message } from '@/components/Message'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

// 导入发送验证码方法
import { useSendCode } from '@/hooks/page'

const useValidateForm = () => {
  //  表单所有对象数据
  const formData = reactive({
    account: null, // 账号
    password: null, // 密码
    code: null,
    mobile: null,
    rePassword: null
  })

  // 表单校验规则
  const rules = {
    account: myRules.accountApi,
    password: myRules.password,
    code: myRules.code,
    mobile: myRules.mobile,
    rePassword: myRules.rePassword
  }

  return { formData, rules }
}

// QQ号注册小兔鲜儿账号
const useSubmit = (fm, props, formData) => {
  const store = useStore()
  const router = useRouter()

  // 点击注册账号事件回调
  const submit = async () => {
    /**
     * 1. 表单整体校验
     * 2. 校验通过执行绑定=》获取登录人信息存储=》跳首页
     */
    const { valid } = await fm.value.validate()
    if (!valid) return
    try {
      // 校验通过
      const { result } = await userQQPatchLogin({ unionId: props.unionId, ...formData })
      // 存储当前登录人信息
      await store.commit('user/setToken', result)
      // 登录成功之后 =》 合并之前本地购物车数据，到登录人名下
      await store.dispatch('cart/mergeCartAction')
      message({ type: 'success', text: '注册成功！' })
      router.replace('/')
    } catch (error) {
      message({ type: 'error', text: '注册失败！' })
    }
  }
  return { submit }
}

export default {
  name: 'CallbackPatch',
  props: {
    // 用户标识
    unionId: {
      type: String,
      default: ''
    }
  },
  components: {
    Form,
    Field
  },
  setup (props) {
    // 表单验证
    const { formData, rules } = useValidateForm(0)
    // 点击发短息验证码
    const { handlerCode, fm, count } = useSendCode(formData, userQQPatchCode)
    // 立即注册绑定
    const { submit } = useSubmit(fm, props, formData)
    return { formData, rules, handlerCode, fm, count, submit }
  }

}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
